package com.FTW.contoller;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/rag")
public class RAGController {
    @Resource
    private ChatClient chatClient;

    @Resource
    private DashScopeApi dashScopeApi;

    @GetMapping("/dochat")
    public Flux<String> doChat(@RequestParam(name = "msg",defaultValue = "888") String message) {
        DocumentRetriever dr = new DashScopeDocumentRetriever(dashScopeApi,
                DashScopeDocumentRetrieverOptions.builder()
                        .withIndexName("RAG运维助手")
                        .build());

        return chatClient.prompt()
                .user(message)
                .advisors(new DocumentRetrievalAdvisor(dr))
                .stream()
                .content();
    }
}
